﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess
{
    /// <summary>
    /// 订单缓存类
    /// </summary>
   public static class OrderRedis
    {
       /// <summary>
       /// 订单缓存Key
       /// </summary>
       public const string OrderKey = "Order_Key_";
       /// <summary>
       /// 插入当天实时订单数据到缓存
       /// </summary>
       /// <param name="value"></param>
       /// <param name="mamaid"></param>
       /// <param name="promoter_id"></param>
       /// <returns></returns>
       public static bool SetOrderList(string value, string mamaid, string promoter_id)
       {
           string key = string.Format(OrderKey, DateTime.Now.ToString("yyyyMMdd"), promoter_id);
           //存在直接覆盖
           return RedisHelper.Hash_Set(key, mamaid, value);
       }
       /// <summary>
       /// 通过推广者ID获取今天所有阿里妈妈账号下的订单集合
       /// </summary>
       /// <param name="promoter_id">推广者ID</param>
       /// <returns></returns>
       public static List<Models.Orders> GetOrderAllList(string promoter_id)
       {
           string key = string.Format(OrderKey, DateTime.Now.ToString("yyyyMMdd"), promoter_id);
          
           List<string> list = RedisHelper.GetValuesFromHash(key);
           List<Models.Orders> orderList = new List<Models.Orders>();
           foreach (var item in list)
           {
                orderList.AddRange(ServiceStack.Text.JsonSerializer.DeserializeFromString<List<Models.Orders>>(item));
           }
           return orderList;
       }
       /// <summary>
       /// 通过推广者ID+阿里妈妈ID获取指定的妈妈账号数据
       /// </summary>
       /// <param name="promoter_id">推广者ID</param>
       /// <param name="mamaid">阿里妈妈ID</param>
       /// <returns></returns>
       public static List<Models.Orders> GetOrderList(string promoter_id,string mamaid)
       {
           string key = string.Format(OrderKey, DateTime.Now.ToString("yyyyMMdd"), promoter_id);
           return RedisHelper.Hash_Get<List<Models.Orders>>(key, mamaid);
       }
       /// <summary>
       /// 根据代理推广位账号查询代理今日订单
       /// </summary>
       /// <param name="promoter_id">推广者ID</param>
       /// <param name="mamaid">阿里妈妈ID</param>
       /// <param name="AgentID">代理ID（代理推广账号））</param>
       /// <returns></returns>
       public static List<Models.Orders> GetAgentOrderList(string promoter_id, string mamaid,string AgentID)
       {
           string key = string.Format(OrderKey, DateTime.Now.ToString("yyyyMMdd"), promoter_id);
           return RedisHelper.Hash_Get<List<Models.Orders>>(key, mamaid).FindAll(x => x.AdvName == AgentID).OrderByDescending(c => c.CreateDate).ToList();

       }
    }
}
